package com.rfid.afd.presenter;

import android.os.Handler;
import android.text.TextUtils;

import com.rfid.afd.activity.listener.ModelListener;
import com.rfid.afd.activity.view.ICommonUiView;
import com.rfid.afd.model.BaseUiModel;
import com.rfid.afd.model.DeleteModel;
import com.rfid.config.ConfigManager;
import com.rfid.db.bean.Operation;
import com.rfid.db.bean.Rfid;
import com.rfid.util.Convert;
import com.rfid.util.LogUtils;
import com.rfid.util.RfidManager;
import com.rfid.util.DBManager;
import com.rfid.util.RfidUitl;
import com.rfid.util.UserManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BaseUiPresenter<T extends ICommonUiView> extends BasePresenter<T> implements ModelListener {

    protected BaseUiModel mBaseModel;
    protected ExecutorService mExecutor;
    protected Handler mHandler;
    private List<Operation> mOperations;

    public BaseUiPresenter() {
        mBaseModel = getModel();
        mHandler = new Handler();
        mExecutor = Executors.newSingleThreadExecutor();
        mBaseModel.setRfidListener(this);
        mOperations = new ArrayList<>();
        mBaseModel.setDisableShelf(false);
    }

    @Override
    public void detach() {
        super.detach();
        mBaseModel.setRfidListener(null);
    }

    public void setDisableShelf(boolean disableShelf) {
        mBaseModel.setDisableShelf(disableShelf);
    }

    protected BaseUiModel getModel() {
        return new BaseUiModel();
    }

    public void startScanRfid() {
        if(mBaseModel != null)
            mBaseModel.startScan();
    }

    public void stopScanRfid() {
        if(mBaseModel != null)
            mBaseModel.stopScan();
    }

    @Override
    public void onShelf(String shelf) {
        mIview.onShelf(Convert.decryBarcode(shelf));
    }

    @Override
    public void onRfid(String rfid) {
        if(TextUtils.isEmpty(rfid)) {
            return;
        }
        System.out.println("rfid = " + rfid);
        Rfid r = DBManager.queryRfidByUid(rfid);
        if(r == null) {
            if(ConfigManager.getInstance().getConfig().isShowAll()) {
                if(RfidUitl.isLevelBarcode(RfidUitl.decodeLevelEpc(rfid))) {
                    return;
                }
                r = new Rfid();
                r.setUid(rfid);
                r.setBarcode(RfidUitl.decodeBookEpc(rfid));
            } else {
                return;
            }
        }
        if(mIview == null) {
            return;
        }
        Rfid finalR = r;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onScaned(rfid, finalR);
            }
        });
    }

    public void getLocatedData(String shelf) {
        mIview.showLoading();
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<Rfid> data = mBaseModel.getLocatedRfid(shelf);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIview.stopLoading();
                        mIview.onRfidsQueryedByShelf(data);
                    }
                });
            }
        });
    }

    public void queryDeleteRfids() {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<Rfid> deletes = mBaseModel.getRemoteDeleteRfids();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIview.onDeleteQuery(deletes);
                    }
                });
            }
        });
    }


    public void addOperationLog(Rfid rfid,int operate) {
        Operation operation = new Operation();
        operation.setOperation(operate);
        operation.setRfid(rfid.getId());
        operation.setDate(Util.getDateTime());
        operation.setUser(UserManager.getInstance().getLoginUser().getName());
        operation.setStatus(rfid.getFlag() == Rfid.FLAG_ERR_SHELF ? 1 : 0);
        if(!mOperations.contains(operation)) {
            mOperations.add(operation);
        }
    }

    public void updateOperation() {
        for(Operation operation : mOperations) {
            DBManager.insertOperation(operation);
        }
        mOperations.clear();
    }

    @Override
    public void onRfid(String epc, String barcode) {
    }
}
